﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Nightlife.Models;

namespace Nightlife.Controllers
{
    public class SpotController : BaseController
    {

        //
        // GET: /Spot/

        public ActionResult Index()
        {
            return View(DB.GetAllSpots());
        }

        //
        // GET: /Spot/Details/5

        public ActionResult Details(int id = 0)
        {
            SpotModel spotmodel = DB.GetSpot(id);
            if (spotmodel == null)
            {
                return HttpNotFound();
            }
            return View(spotmodel);
        }

        //
        // GET: /Spot/Create

        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Spot/Create

        [Authorize]
        [HttpPost]
        public ActionResult Create(SpotModel spotmodel)
        {
            if (ModelState.IsValid)
            {
                spotmodel.Created = DateTime.Now;

                DB.Add(spotmodel);
                return RedirectToAction("Index");
            }

            return View(spotmodel);
        }

        //
        // GET: /Spot/Edit/5

        [Authorize]
        public ActionResult Edit(int id = 0)
        {
            SpotModel spotmodel = DB.GetSpot(id);
            if (spotmodel == null)
            {
                return HttpNotFound();
            }
            return View(spotmodel);
        }

        //
        // POST: /Spot/Edit/5

        [Authorize]
        [HttpPost]
        public ActionResult Edit(SpotModel spotmodel)
        {
            if (ModelState.IsValid)
            {
                DB.Update(spotmodel);
                return RedirectToAction("Index");
            }
            return View(spotmodel);
        }

        //
        // GET: /Spot/Delete/5
        [Authorize(Roles="Admin")]
        public ActionResult Delete(int id = 0)
        {
            SpotModel spotmodel = DB.GetSpot(id);
            if (spotmodel == null)
            {
                return HttpNotFound();
            }
            return View(spotmodel);
        }

        //
        // POST: /Spot/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            SpotModel spotmodel = DB.GetSpot(id);
            DB.Delete(spotmodel);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            DB.Dispose();
            base.Dispose(disposing);
        }
    }
}